#include "cfg.h"
#include <QDebug>
#include <QDir>
#include <QTextCodec>

configFile::configFile()
{
    m_psetting = nullptr;
}

bool configFile::InitFile(QString qstrfilename)
{
    if (qstrfilename.isEmpty())
    {
        return false;
    }
    else
    {
        m_qstrFileName = qstrfilename;
    }
    QDir dir;
    if (!dir.exists(m_qstrFileName)) {
        qDebug() << "cfg" << m_qstrFileName << "  is not exist ";
        return false;
    }
    m_psetting = new QSettings(m_qstrFileName, QSettings::IniFormat);
    m_psetting->setIniCodec(QTextCodec::codecForName("utf-8"));
    return true;
}

configFile::~configFile()
{
    if (m_psetting)
    {
        delete m_psetting;
        m_psetting = nullptr;
    }

}

void configFile::Set(QString qstrNodeName, QString qstrKeyName, QVariant qvarvalue)
{
    m_psetting->setValue(QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName), qvarvalue);
}

QVariant configFile::Get(QString qstrNodeName, QString qstrKeyName)
{
    QVariant qvar = m_psetting->value(QString("/%1/%2").arg(qstrNodeName).arg(qstrKeyName));
    return qvar;
}
